package com.zhz.crm.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AuthorizationFilter implements Filter {

//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//
//    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest HttpRequest = (HttpServletRequest) request;
        HttpServletResponse HttpResponse = (HttpServletResponse) response;
        if(HttpRequest.getSession().getAttribute("loginUser")==null){
            HttpResponse.setContentType("text/html;charset=UTF-8");
            PrintWriter out = HttpResponse.getWriter();
            out.print("<script>alert('请先进行登录，再进行后续操作！(Filter控制)');location.href='"
                    +HttpRequest.getContextPath()+"/login.jsp';</script>");
        }else{
            filterChain.doFilter(HttpRequest,HttpResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
